//
//  CSYVisitorView.swift
//  CSSinaMicroBlog_Test
//
//  Created by 姚彦兆 on 15/11/11.
//  Copyright © 2015年 姚彦兆. All rights reserved.
//

import UIKit
 /// 设置代理

@objc protocol CSYVisitorViewDelegate : NSObjectProtocol{

    optional func didClickResignButton()
    
    optional func didClickLoginButton()

}

/// 创建访客视图

/// 首先重写初始化方法,然后懒加载控件
class CSYVisitorView: UIView {

    var delegate : CSYVisitorViewDelegate?
    
    @objc func wantToLogin(){
    
        delegate?.didClickLoginButton?()
    
    }
    
    @objc func wantToRegister(){
    
        delegate?.didClickResignButton?()
    
    }
    
    override func reloadInputViews() {
        
        
    }
    
    /**
     给外界提供一个设置载入图片的方法
     */
    func setItems( imageName : String? , tipsInfo : String ){
    
        tipsLabel.text = tipsInfo
        
        insideView.hidden = false
        
        if imageName != nil{
            
            bringSubviewToFront(outIconView)
        
            outIconView.image = UIImage(named: imageName!)
            
            insideView.hidden = true
            
        }else{
        
            startAnimation()
        
        }
    
    }
    
    /**
     播放首页动画
     */
     
    private func startAnimation(){
    
        let animation : CABasicAnimation = CABasicAnimation(keyPath: "transform.rotation")
        
        animation.toValue = 2 * M_PI
        
        animation.duration = 20
        
        animation.repeatCount = MAXFLOAT
        
        animation.removedOnCompletion = false
        
        outIconView.layer.addAnimation(animation, forKey: nil)
        
//        insideView.layer.addAnimation(animation, forKey: nil)
    
    }
     
    
    /**
     加载UI控件
     */
    private func setUpUI(){
    
        addSubview(outIconView)
        
        addSubview(coverView)
        
        addSubview(insideView)
        
        addSubview(tipsLabel)
        
        addSubview(loginButton)
        
        addSubview(registerButton)
        
        for subView in subviews{
        
            subView.translatesAutoresizingMaskIntoConstraints = false 
        
        }
        
        
        /**
        设置主要约束
        
        - returns: 设置主要约束
        */
        
        //ICON约束
        addConstraint(NSLayoutConstraint(item: outIconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0.0))
        
        addConstraint(NSLayoutConstraint(item: outIconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: -60))
        
        //Inside约束
        addConstraint(NSLayoutConstraint(item: insideView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: outIconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0.0))
        
        addConstraint(NSLayoutConstraint(item: insideView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: outIconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0.0))
        
        //TipsLabel约束
        addConstraint(NSLayoutConstraint(item: tipsLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: outIconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 5))
        
        addConstraint(NSLayoutConstraint(item: tipsLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0.0))
        
        addConstraint(NSLayoutConstraint(item: tipsLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 224))
        
        addConstraint(NSLayoutConstraint(item: tipsLabel, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 42))
        
        //Button约束
        //LoginButton
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: tipsLabel, attribute: NSLayoutAttribute.Left, multiplier: 1.0, constant: 0.0))
        
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: tipsLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 10))
        
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 100))
        
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 35))
        
        //RegisterButton
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: tipsLabel, attribute: NSLayoutAttribute.Right, multiplier: 1.0, constant: 0.0))
        
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: tipsLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 10))
        
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 100))
        
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 35))
        
        //设置遮罩View的约束
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[coverView]-0-|", options: [], metrics: nil, views: ["coverView" : coverView]))
        
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[coverView]-(-35)-[loginButton]", options: [], metrics: nil, views: ["coverView" : coverView , "loginButton" : loginButton]))
    
        self.loginButton.addTarget(self, action: "wantToLogin", forControlEvents: UIControlEvents.TouchUpInside)
        
        self.registerButton.addTarget(self, action: "wantToRegister", forControlEvents: UIControlEvents.TouchUpInside)
        
    }
    
//    init(){
//    
//        super.init(frame: CGRectZero)
//    
//        setUpUI()
//        
//    }
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        self.backgroundColor = UIColor(white: 0.93, alpha: 1)
        
        setUpUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /**
    *  懒加载所有控件
    */

//     lazy var outIconView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    lazy var outIconView : UIImageView = {
    
        let view : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        
        return view
    
    }()
    
    lazy var insideView : UIImageView = {
    
        let view : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        
        return view
        
    }()
    
    lazy var coverView : UIImageView = {
    
        let view : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        
        return view
        
    }()
    
    lazy var tipsLabel : UILabel = {
    
        let label : UILabel = UILabel()

        label.text = "关注一些人,然后回这里看看有什么惊喜"
        
        label.textColor = UIColor.lightGrayColor()
        
        label.textAlignment = .Center

        label.font = UIFont.systemFontOfSize(14)

        label.numberOfLines = 0 
        
        return label
        
    }()
    
    lazy var loginButton : UIButton = {
    
        let btn : UIButton = UIButton()
        
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        btn.setTitle("登陆", forState: UIControlState.Normal)
        
        btn.setTitleColor(UIColor.lightGrayColor(), forState: UIControlState.Normal)
        
        return btn
        
    }()
    
    lazy var registerButton : UIButton = {
        
        let btn : UIButton = UIButton()
        
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        btn.setTitle("注册", forState: UIControlState.Normal)
        
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        return btn
        
    }()
    
    /**
    单例方法
    */
    
    static let visitor: CSYVisitorView = { () -> CSYVisitorView in
        let instance = CSYVisitorView()
        return instance
    }()
    
    
}
